import { observable, action, toJS } from 'mobx';
import { WeaForm, WeaTableNew, WeaLogView } from "comsMobx";
import { WeaLocaleProvider } from "ecCom";
import { message } from 'antd';
import * as API from "../apis/index.js"
import { ITEMS } from '../static/index';

const { TableStore } = WeaTableNew;
const { LogStore } = WeaLogView;
const getLabel = WeaLocaleProvider.getLabel;

class demo {
  @observable form = new WeaForm();
  @observable editForm = new WeaForm();
  @observable searchForm = new WeaForm();
  @observable inputValue = '';
  @observable status = {
    showSearchAd: false,
    advancedSearchConditionInfo: []
  }
  @observable tableStore = new TableStore();
  @observable logStore = new LogStore();
  @observable visible = false;
  @observable logVisible = false;
  @observable editVisible = false;
  @observable currentId = '';
  @observable condition = [{
    "title": "",
    "defaultshow": true,
    "items": ITEMS
  }]

  @observable selectedKey = 0;

  @observable reportDatas = [];

  @action
  changeSelectdKey(sk) {
    this.selectedKey = sk;
  }

  @action
  callReportData(params) {
    //promise
    API.getSystemInfo(params).then(res => {
      // console.log('res:',res);
      if(res.status) {
        this.reportDatas = res.datas;
      }
    });
  }

  @action
  doInit = () => {
    this.searchForm = new WeaForm();
    this.getCondition();
    this.form.setCondition(this.condition);
    this.getSystemInfo();
    this.getTable(true);
  }

  @action
  getSystemInfo = (params) => {
    API.getSystemInfo(params).then(res => {
      const datas = res.datas || {};
      this.form.updateFields({
        "em_corpid": res["em_corpid"] || "",
        "ec_name": res["ec_name"] || "",
        "ec_url": res["ec_url"] || "",
        "ec_url_open": res["ec_url_open"] || "",
        "em_url": res["em_url"] || "",
        "em_url_open": res["em_url_open"] || "",
        "ec_id": res["ec_id"] || "",
        "id": res["id"] || "0"
      });
    })
  }

  @action
  join = () => {
    const params = this.form.getFormParams();
    API.join(params).then(res => {
      this.setVisible(false);
      this.getTable();
      message.success(getLabel(18758, '保存成功'));
    }, error => {
      message.warning(error.message);
    })
  }

  @action
  update = () => {
    const params = this.editForm.getFormParams();
    API.update(params).then(res => {
      this.setEditVisible(false);
      this.getTable();
      message.success(getLabel(18758, '保存成功'));
    }, error => {
      message.warning(error.message);
    })
  }

  @action
  deleteRecord = (params) => {
    let param = { ids: params.id };
    API.deleteRecord(param).then(res => {
      if (res) {
        this.getTable();
        message.success(getLabel(20461, '删除成功'));
      }
    }, error => {
      message.warning(error.message);
    })
  }

  @action
  changeStatus = (params) => {
    API.changeStatus(params).then(res => {
      if (res) {
        this.getTable();
      }
    }, error => {
      message.warning(error.message);
    })
  }

  @action
  getCondition = () => {
    API.getAdvancedSearchCondition().then(res => {
      this.status = {
        ...this.status,
        advancedSearchConditionInfo: res.conditions
      }
      this.searchForm.initFormFields(res.conditions);
    });
  }

  @action
  editFormInit = (params) => {
    API.editLable(params).then(res => {
      if (res) {
        this.editForm.setCondition(this.condition);
        this.editForm.updateFields(res, true);
      }
    }, error => {
      message.warning(error.message);
    });
  }

  @action
  setPanelStatus = (bool) => {
    this.status = {
      ...this.status,
      showSearchAd: bool
    }
  }
  updateInputVal(val = '') {
    this.inputValue = val;
  }

  @action
  getTable = (params) => {
    API.getTable({ ...this.searchForm.getFormParams() }).then(res => {
      if (res.datas) {
        if (params) {
          this.tableStore.getDatas(res.datas, 1);
        } else {
          this.tableStore.getDatas(res.datas);
        }
      } else {
        res.message && message.warning(res.message);
      }
    }, error => {
      message.warning(error.message);
    })
  }

  setVisible(bool) {
    this.visible = bool;
  }

  setLogVisible(bool) {
    this.logVisible = bool;
  }

  setEditVisible(bool) {
    this.editVisible = bool;
  }
  setCurrentId(param) {
    this.currentId = param;
  }
}

export default demo;
